home *** CD-ROM | disk | FTP | other *** search
/ Java Certification Exam Guide / McGrawwHill-JavaCertificationExamGuide.iso / pc / Web Links and Code / code / chap18 / Ex3.java < prev    next >
Encoding:
Java Source  |  1997-04-20  |  1.1 KB  |  48 lines

  1. import java.awt.*;
  2. import java.applet.Applet;
  3.  
  4. public class Ex3 extends Applet { 
  5.    Color         color = Color.red;
  6.    int           candidate = 3;
  7.  
  8.    public void init() {
  9.  
  10.       class PrimeThread extends Thread {
  11.  
  12.          public void run() {
  13.             for ( ; ; candidate++) {
  14.                if (isPrime(candidate)) 
  15.                   color = Color.red;
  16.                else
  17.                   color = Color.blue;
  18.  
  19.                repaint();
  20.  
  21.                try {
  22.                   sleep(1000);
  23.                } catch (InterruptedException ie) {
  24.                }         
  25.             }
  26.          }
  27.  
  28.          public boolean isPrime(int number) {
  29.             boolean isPrime = true;
  30.     
  31.             for (int i = 2; i < number - 1 && isPrime; i++) {
  32.                if ( (number % i ) == 0)
  33.                   isPrime = false;
  34.             }
  35.             return isPrime;
  36.          }
  37.       }
  38.  
  39.       new PrimeThread().start();
  40.    }
  41.  
  42.    public void paint(Graphics g) {
  43.       g.setColor(color);
  44.       g.drawString(new Integer(candidate).toString(), 30, 40);
  45.    }
  46.  
  47. }
  48.